<?php

/**
 * NOTICE OF LICENSE
 *
 * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
 * 
 * DO NOT USE THIS FILE IN OTHER PLACE.
 *
 * @package   App_Backend
 * @author    Cheng Wei <berlios.o@gmail.com>
 * @version   $$Id: ErrorController.php 20 2010-12-18 09:35:07Z cheng.wei $$
 * @copyright Copyright (c) 2008 Evebit Inc. China (http://www.evebit.com)
 */

/**
 * Derfault controller for error
 * 
 * @package   App_Backend
 * @author    Cheng Wei <berlios.o@gmail.com>
 * @version   $$Id: ErrorController.php 20 2010-12-18 09:35:07Z cheng.wei $$
 * 
 * @see Zend_Controller_Plugin_ErrorHandler
 */
class User_ErrorController extends Zend_Controller_Action {
	
	/**
	 * Error handle
	 */
	public function errorAction() {
		$errors = $this->_getParam('error_handler');
		$exception = $errors->exception;
		
		switch ($errors->type) {
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER :
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION :
				
				// 404 error -- controller or action not found
				$this->getResponse()->setHttpResponseCode(404);
				$message = 'Page not found';
				break;
				
			default :
				if ($exception->getCode() == 404) {
					// 404 error -- page not found
					$this->getResponse()->setHttpResponseCode(404);
					$message = 'Page not found';
				} else {
					$this->getResponse()->setHttpResponseCode(500);
					$message = 'Application error';
				}
				break;
		}
		
		$this->view->message = $message;
		$this->view->exception = $exception;
		
		$this->view->request = $errors->request;
		
		$log = $this->getLog();
		if ($log) {
			$log->err($exception);
		}
	}
	
	/**
	 * Get global log
	 * @return Zend_Log
	 */
	public function getLog() {
		$bootstrap = $this->getInvokeArg('bootstrap');
		if (!$bootstrap->hasPluginResource('Log')) {
			return false;
		}
		$log = $bootstrap->getResource('Log');
		return $log;
	}
}

